package x.ovo.wechat.bot.http.request.core;

import cn.hutool.core.net.url.UrlBuilder;
import cn.hutool.json.JSONUtil;
import x.ovo.wechat.bot.common.Constant;
import x.ovo.wechat.bot.common.http.request.ApiRequest;
import x.ovo.wechat.bot.common.http.session.LoginSession;
import x.ovo.wechat.bot.http.response.StatusNotifyResponse;

import java.util.Map;
import java.util.Objects;
import java.util.function.Function;

/**
 * 状态通知请求
 *
 * @author ovo, created by 2024/05/03
 */
public class StatusNotifyRequest extends ApiRequest<StatusNotifyResponse> {

    private final String contactUserName;

    public StatusNotifyRequest(LoginSession session, String contactUserName) {
        super(session);
        this.contactUserName = contactUserName;
    }

    @Override
    public String getMethod() {
        return POST;
    }

    @Override
    public boolean isJson() {
        return true;
    }

    @Override
    public String getUrl() {
        return UrlBuilder.of(this.session.getUrl())
                .addPath(Constant.STATUS_NOTIFY)
                .addQuery("lang", "zh_CN")
                .addQuery("pass_ticket", this.session.getPassTicket())
                .build();
    }

    @Override
    public Map<String, Object> getParameters() {
        return JSONUtil.createObj()
                .set("BaseRequest", this.session.getBaseRequest())
                .set("ClientMsgId", System.currentTimeMillis())
                .set("Code", Objects.isNull(this.contactUserName) ? 3 : 1)
                .set("FromUserName", this.session.getUserName())
                .set("ToUserName", Objects.isNull(this.contactUserName) ? this.session.getUserName() : this.contactUserName)
                .getRaw();


//        super.addParameter("BaseRequest", this.baseRequest)
//                .addParameter("ClientMsgId", System.currentTimeMillis())
//                .addParameter("Code", 3)
//                .addParameter("FromUserName", this.session.getUserName())
//                .addParameter("ToUserName", this.session.getUserName());
//        return super.getParameters();
    }

    @Override
    public Function<String, StatusNotifyResponse> responseHandler() {
        return s -> JSONUtil.toBean(s, StatusNotifyResponse.class);
    }

}
